package com.kaifamiao.listener;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet( "/request/attribute/*" )
public class RequestAttributeServlet extends HttpServlet {

    @Override
    protected void doPost( HttpServletRequest request , HttpServletResponse response ) throws ServletException, IOException {
        // 从表单中获取请求参数
        String name = request.getParameter( "name" );
        String value = request.getParameter( "value" );

        // 在 请求对象 中添加属性后会调用 ServletRequestAttributeListener 的 attributeAdded 方法
        request.setAttribute( name , value );

        // 替换 请求对象 中的属性值后会调用 ServletRequestAttributeListener 的 attributeReplaced 方法
        request.setAttribute( name , Math.random() );

        // 从 请求对象 中删除属性后会调用 ServletRequestAttributeListener 的 attributeRemoved 方法
        request.removeAttribute( name );

        response.sendRedirect( request.getContextPath() + "/index.jsp" );
    }

}
